package com.kuang.gulimall.product.controller;

import com.kuang.common.utils.PageUtils;
import com.kuang.common.utils.R;
import com.kuang.gulimall.product.entity.ProductAttrValueEntity;
import com.kuang.gulimall.product.service.AttrService;
import com.kuang.gulimall.product.service.ProductAttrValueService;
import com.kuang.gulimall.product.vo.AttrRespVo;
import com.kuang.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品属性
 *
 * @author xiangmou
 * @email 1140968804@qq.com
 * @date 2021-05-05 18:18:48
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService attrValueService;

    @PostMapping("/update/{spuId}")
    public R updateList(@PathVariable Long spuId,
                        @RequestBody List<ProductAttrValueEntity> entities){

        attrValueService.UpdateSpuAttr(spuId,entities);
        return R.ok();
    }




    @GetMapping("/base/listforspu/{spuId}")
    public R listforspu(@PathVariable Long spuId){
        List<ProductAttrValueEntity> data =  attrValueService.baseAttrlistforspu(spuId);
        return R.ok().put("data",data);
    }

    //sale list
    @GetMapping("/{attrType}/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId,
                  @PathVariable("attrType") String type) {
        PageUtils page = attrService.queryBaseAttrPage(params,catelogId,type);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

//    @RequestMapping("/base/list/{catelogId}")
//    public R list(@RequestParam Map<String,Object> params,
//                  @PathVariable("catelogId") Long catelogId) {
//
//        //这个地方用户分页插件接受数据，可以自动分页
//        PageUtils page = attrService.queryBaseAttrPage(params,catelogId, type);
//        return R.ok().put("page",page);
//    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId) {
//        AttrEntity attr = attrService.getById(attrId);
        AttrRespVo attr = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    //这里保存时，我们需要同步保存来刷新中间表
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr) {
        attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr) {
//        attrService.updateById(attr);
        attrService.updateAttr(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
